package com.excelcreatetable.demo.dbconnect;/**
 * @author wzj
 * @date 2020/3/17 18:01
 * @version 1.0
 */

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

/**
 * auther wzj
 * date 2020/3/17 18:01
 */
public class DBConnect {



    private static String driver;
    private static String url;
    private static String userName;
    private static String passWord;
    private static Connection connection;

    /**
     * 获取数据库连接
     * @return
     */
    public static Connection getConnection(){
        Properties properties = new Properties();
        InputStream inputStream = DBConnect.class.getClassLoader().getResourceAsStream("db.properties");
        try {
            properties.load(inputStream);
            url = properties.getProperty("db.url");
            driver = properties.getProperty("db.driver");
            userName = properties.getProperty("db.username");
            passWord = properties.getProperty("db.password");
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            Class.forName(driver);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        try {
            connection = DriverManager.getConnection(url,userName,passWord);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }
}
